import type {
  RouteRecordRaw,
} from "./types.d";
import {
  transformRoutesToNested,
  get_Routes,
} from "./toolFuncs";
import { directory_map } from "./constants";

/**
 * - 根据views目录转换为路由配置表
 * @param {string} directory 目录
 * @returns
 */
export const menu2Routes = (directory_list: (keyof typeof directory_map)[]) => {
  // - 总路由列表
  const routes: RouteRecordRaw[] = [];

  // - 处理参数
  if (!directory_list.length) {
    throw new Error("请指定需要转换的目录");
  } else {
    // try {
      // - 遍历目录
      directory_list.forEach((item) => {
        // - 获取组件内容
        // console.log("item: ", item);
        const route = get_Routes(item as keyof typeof directory_map);
        routes.push(...route);
        // console.log("routes: ", routes);
      });
    // } catch (err) {
    //   throw new Error(JSON.stringify(err));
    // }
  }

  // console.log("flatRoutes: ", routes);

  return transformRoutesToNested(routes);
};
